home *** CD-ROM | disk | FTP | other *** search
/ MPEG Toolkit / MPEG Toolkit.iso / dos / secmpeg3 / stream.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  5.7 KB  |  292 lines

  1.  
  2. /* --- C ---
  3. ************************************************************************
  4. *
  5. *    Filename    : stream.c
  6. *    Description : General stream-i/o-functions
  7. *    Part of     : SECMPEG
  8. *
  9. *    Version     : 1.0
  10. *    Language    : C
  11. *    For machine : SunOS 4.1.x, INTERACTIVE Unix 2.2.1, Linux, MS-DOS
  12. *    Compile as  : see Makefile
  13. *
  14. *    Authors     : Juergen Meyer, Frank Gadegast
  15. *    Contact     : jm@cs.tu-berlin.de, phade@cs.tu-berlin.de
  16. *
  17. ************************************************************************
  18. */
  19.  
  20. #include "defs.h"
  21. #include <fcntl.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <errno.h>
  25. #include <stddef.h>
  26. #ifdef DOS
  27. #ifdef GNU
  28.    #include <malloc.h>
  29. #else
  30.    #include <alloc.h>
  31.    #include <io.h>
  32. #endif
  33. #else
  34.    #include <malloc.h>
  35. #endif
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38.  
  39.  
  40. /* ------ DEFINES ------ */
  41.  
  42. #ifdef DOS
  43. #define INPUT_ACCESS  O_BINARY|O_RDONLY
  44. #define OUTPUT_ACCESS O_BINARY | O_CREAT|O_TRUNC | O_RDWR
  45. #else
  46. #define INPUT_ACCESS  O_RDONLY
  47. #define OUTPUT_ACCESS O_CREAT|O_TRUNC | O_RDWR
  48. #endif
  49. #define OUTPUT_PERMS  S_IWRITE|S_IREAD
  50.  
  51. /* ------ PUBLIC ------ */
  52.  
  53. int     InitMemory();
  54. void    FreeMemory();
  55.  
  56. int     OpenInputStream(char *);
  57. int     OpenOutputStream(char *);
  58. void    CloseStreams();
  59. int     WriteStream();
  60. int     ReadStream(unsigned long);
  61. void    CopyBuffer();
  62.  
  63. int     GetByte();
  64. int     _GetnBit(int);
  65. int     _GetBit();
  66.  
  67. void    readalign();
  68. int     CheckEOF();
  69.  
  70.  
  71. /* ------ VARS ------ */
  72.  
  73. extern short ENCODE;
  74. extern int   WRITE;
  75.  
  76. int input         = 0;
  77. int output        = 0;
  78. int end_of_file   = 0;
  79. unsigned long total_bytes_read    = 0L;
  80. unsigned long total_bytes_written = 0L;
  81.  
  82. BYTE *io_buffer    = NULL;
  83. BYTE *frame_buffer = NULL;
  84.  
  85. static unsigned long io_buffer_end   = 0;
  86. static unsigned long io_buffer_start = 0;
  87. static unsigned long frame_buffer_end   = 0;
  88.  
  89. static unsigned long io_buffersize = IO_BUFFERSIZE;
  90.  
  91. static INT current_write_byte;                /* variablen und array      */
  92. static INT current_read_byte;                 /* mⁿssen nicht 32 Bit sein */
  93. static INT read_position;                     /* noch zu ⁿberprⁿfen !!!!! */
  94. static INT write_position;                    /* 8 Bit sollten genug sein */
  95.                                               /* timecode --> 25 Bit GOP  */
  96.  
  97. INT bit_set_mask[] =
  98. {0x00000001,0x00000002,0x00000004,0x00000008,
  99. 0x00000010,0x00000020,0x00000040,0x00000080,
  100. 0x00000100,0x00000200,0x00000400,0x00000800,
  101. 0x00001000,0x00002000,0x00004000,0x00008000,
  102. 0x00010000,0x00020000,0x00040000,0x00080000,
  103. 0x00100000,0x00200000,0x00400000,0x00800000,
  104. 0x01000000,0x02000000,0x04000000,0x08000000,
  105. 0x10000000,0x20000000,0x40000000,0x80000000};
  106.  
  107.  
  108. int     InitMemory()
  109. {
  110.     io_buffer = (BYTE *) malloc(IO_BUFFERSIZE);
  111.     if(!io_buffer)
  112.        return(ERROR_MEMORY);
  113.     frame_buffer = (BYTE *) malloc(FRAME_BUFFERSIZE);
  114.     if(!frame_buffer)
  115.        return(ERROR_MEMORY);
  116.     return(TRUE);
  117. }
  118.  
  119. void    FreeMemory()
  120. {
  121.     if(frame_buffer)
  122.        free(frame_buffer);
  123.     if(io_buffer)
  124.        free(io_buffer);
  125.  
  126.     io_buffer    = NULL;
  127.     frame_buffer = NULL;
  128.     return;
  129. }
  130.  
  131.  
  132. int    OpenInputStream (char *StreamName)
  133. {
  134.     long bytes_read = 0;
  135.  
  136. #ifdef DEBUG
  137.     fprintf(stderr,"OpenInputStream \n");
  138. #endif
  139.  
  140.     if (strcmp (StreamName, "-"))
  141.         input = open(StreamName,INPUT_ACCESS);
  142.     else input = 0;
  143.     if(input < 0) return (ERROR_OPEN_INPUT);
  144.     if (ENCODE != M_ENCODE) return (input);
  145.     bytes_read = read (input,(void *)io_buffer, IO_BUFFERSIZE);
  146.     if (bytes_read != IO_BUFFERSIZE)
  147.     {
  148.         io_buffersize = bytes_read;
  149.         if (!bytes_read) end_of_file = TRUE;
  150.     }
  151.     return (input);
  152. }
  153.  
  154. int    OpenOutputStream (char *StreamName)
  155. {
  156.     if (WRITE == FALSE) return (0);
  157. #ifdef DEBUG
  158.     fprintf(stderr, "OpenOutputStream \n");
  159. #endif
  160.     if (strcmp (StreamName, "-"))
  161.         output = open (StreamName,OUTPUT_ACCESS,OUTPUT_PERMS);
  162.     else output = 1; 
  163.     if (output < 0) return (ERROR_OPEN_OUTPUT);
  164.     return (output);
  165. }
  166.  
  167. void     CloseStreams()
  168. {
  169. #ifdef DEBUG
  170.     fprintf(stderr,"CloseMpegStream \n");
  171. #endif
  172.     if (input) close(input);
  173.     if(output) close(output);
  174. }
  175.  
  176. int    WriteStream()
  177. {
  178.     if (!WRITE) return (TRUE);
  179.     total_bytes_written += frame_buffer_end;
  180.  
  181.     if((write(output,(void *)frame_buffer,(unsigned)frame_buffer_end) == -1))
  182.         return(ERROR_WRITE);
  183.     frame_buffer_end = 0;
  184.     return(TRUE);
  185. }
  186.  
  187. int    ReadStream(unsigned long len)
  188. {
  189.     if((read(input,(void *)frame_buffer,(unsigned)len)) != (unsigned)len)
  190.         return(ERROR_READ);
  191.     frame_buffer_end = len;
  192.     return(TRUE);
  193. }
  194.  
  195. void    CopyBuffer()
  196. {
  197.     unsigned long len;
  198.  
  199.     len = io_buffer_end - io_buffer_start;
  200.         
  201.     while(len--)
  202.        frame_buffer[frame_buffer_end++] = io_buffer[io_buffer_start++];
  203. }
  204.  
  205.  
  206. int     GetByte()
  207. {
  208.     long   bytes_read;
  209.  
  210.     total_bytes_read++;
  211.  
  212.     if (io_buffer_end < io_buffersize)
  213.         return((INT)io_buffer[io_buffer_end++]);
  214.     else
  215.     {
  216.         CopyBuffer();
  217.         io_buffer_start = 0;
  218.         io_buffer_end   = 0;
  219.         bytes_read = (long)read(input,(void *)io_buffer,IO_BUFFERSIZE);
  220.  
  221.         if(bytes_read != IO_BUFFERSIZE)
  222.         {
  223.         io_buffersize = bytes_read;
  224.         if(!bytes_read)
  225.         {
  226.            total_bytes_read -= 1;
  227.            end_of_file = 1;
  228. #ifdef IO_DEBUG
  229.            fprintf(stderr," GetByte --> EOF\n");
  230. #endif
  231.         }
  232.         }
  233.  
  234.         return((INT)io_buffer[io_buffer_end++]);
  235.     }
  236. }
  237.  
  238. void readalign()
  239.   current_read_byte = 0;
  240.   read_position = -1;
  241. #ifdef DEBUG
  242.   fprintf(stderr," readalign\n");
  243. #endif
  244. }
  245.  
  246.  
  247.  
  248. int    _GetBit()
  249. {
  250.  
  251.   if (read_position<0)
  252.     {
  253.       current_read_byte=GetByte();
  254.       read_position = 7;
  255.     }
  256.   if (current_read_byte&bit_set_mask[read_position--]) {return(1);}
  257.   return(0);
  258. }
  259.  
  260.  
  261. int     _GetnBit(int nbits)
  262. {
  263.   int b=0;
  264. #ifdef INT_DEBUG
  265.   if(nbits > 16)
  266.      if(nbits != 24){fprintf(stderr," integer ist unzureichend \n");getch();}
  267. #endif
  268.  
  269.   while(nbits--)
  270.     {
  271.       b <<= 1;
  272.       if (_GetBit()) {b |= 1;}
  273.     }
  274.   return(b);
  275. }
  276.  
  277.  
  278.  
  279. int  CheckEOF()
  280. {
  281.  
  282. #ifdef DEBUG
  283.   fprintf(stderr,"CheckEOF\n");
  284. #endif  
  285.  
  286.   return(end_of_file);
  287. }
  288.  
  289.  
  290.  
  291.